import commonConstants from "@/common/commonConstants";
import MarkerManager from "../marker/MarkerManager";
import TroopManager from "../troop/toorManager";
import TrackManager from "../track/trackManager";
import LayerManager from "../marker/layermanager";
import InfluenceSource from "./influecneSource";
import commonMethod from "@/common/commonMethod";

class ScenarioManager{

    constructor(v,config){

        this.curViewer =v;
        this.scenarioConfiguration = config;
        this.curScenario = undefined;
        this.scenarios = new Map();

        this.monitorManager = undefined;
        this.effectManager = undefined;
    }
    setCurScenario(s){
        this.curScenario =s;
    }

    getCurScenario(){
        return this.curScenario;
    }

    setMonitorAndEffectManager(m,e){
        this.monitorManager = m;
        this.effectManager = e;
    }
    getAllScenarios(){
        let _this = this;
        let scenarioList = [];
        for (let scenarioId of _this.scenarios.keys()) {
            scenarioList.push(_this.scenarios.get(scenarioId));
        }
        scenarioList.sort((s1,s2)=>s1.getCreateTime() < s2.getCreateTime());
        return scenarioList;
    }

    createScenarion(options){
        let _this =this;
        const scenario = new Scenario(_this.curViewer,_this.scenarioConfiguration,_this.monitorManager,_this.effectManager,options);
        _this.addScenario(scenario);
        return scenario;
    }

    addScenario(scenario) {
        let _this = this;
        if (scenario){
            if (!_this.scenarios.has(scenario.getScenarioId())){
                _this.scenarios.set(scenario.getScenarioId(),scenario);

            }
        }
    }

    updateScenario(scenario){
        let _this = this;
        _this.scenarios.set(scenario.getScenarioId(),scenario);
    }
    /**
     *
     * @param scenarioId
     * @return {V|Scenario}
     */
    findScenarioById(scenarioId) {
        let _this = this;
        return _this.scenarios.get(scenarioId);
      
    }

    removeScenarioById(scenarioId) {
        let _this = this;
        let scenario = _this.findScenarioById(scenarioId);
        if (scenario){ 
            scenario.destroy();
            _this.scenarios.delete(scenarioId);
        }
    }
    /**
     * 加载所有想定数据，此是开始时使用的
     */
    loadLocalScenarios(dataHandler) {
        let _this = this;
        //根据不同数据处理类型，来加载想定
        dataHandler.load(informations =>{
            //不论是数据库还是文件，都是存储的信息恢复，具体处理还是要在这里实现
            if (informations) {
               informations.forEach(element => {
                    _this.createScenarion(element);
               });
            }
        })
    }
    /**
     * 部署想定，将态势显示在地球上,此为选中并加载具体想定
     * 此方法只能部署当前想定，无需指定想定去部署
     */
    deployScenario () {
        let _this = this;
        if(_this.curScenario!=undefined){
            //内部的兵力等自行部署
            _this.curScenario.deploy()
        }
    }
}

/**
 * 具体想定管理中才这些个管理者
 */
class Scenario{

    constructor(v,scenarioConfiguration,monitorM,effectM,options){

        this._initOtherManager(v,scenarioConfiguration,monitorM,effectM);

        this.scenarioId = commonMethod.creatUUID();
        this.createTime = new Date();
       
        //想定模式
        this.decisionMaking = commonConstants.deductionModel.rehearsal;
        if (options){
            if (options["id"]){
               this.scenarioId = options["id"];
            }
            if (options["scenarioId"]){
                this.scenarioId = options["scenarioId"];
            }
            if (options["scenarioName"]){
                this.scenarioName = options["scenarioName"];
            }
            if (options["name"]){
                this.scenarioName = options["name"];
            }
            if (options["decisionMaking"]){
                this.decisionMaking = options["decisionMaking"];
            }
            if (options["scenarioDesc"]){
                this.scenarioDescription = options["scenarioDesc"];
            }
            if (options["description"]){
                this.scenarioDescription = options["description"]
            }
            if(options["createTime"]){
                this.createTime = options["createTime"];
            }
        }

        //这个属性是为了直接显示时间而创建
        this.sceanrioCreateTime = commonMethod.dateFormat(this.createTime);

        this._handleConfig(scenarioConfiguration.getConfigurationsByType(commonConstants.dataOperateType.Scenario));
    }

    /**
     * 处理配置的数据
     */
    _handleConfig(configs){
        if (configs) {
             //没有就不处理
            configs.forEach(config => {
                if(config.getKey() == "decisionMaking"){
                    this.decisionMaking = config.getValue();
                }
            });
        }        
    }

    _initOtherManager(v,scenarioConfiguration,monitorM,effectM){
        this.markerManager = new MarkerManager(v,scenarioConfiguration.getConfigurationsByType(commonConstants.dataOperateType.Marker),monitorM);
        this.troopManager = new TroopManager(v,scenarioConfiguration.getConfigurationsByType(commonConstants.dataOperateType.Troop),effectM,monitorM);
        this.trackManager = new TrackManager(v,scenarioConfiguration.getConfigurationsByType(commonConstants.dataOperateType.Track),monitorM);
        this.InfluenceSource = new InfluenceSource(v,scenarioConfiguration.getConfigurationsByType(commonConstants.dataOperateType.Influence));
        if(scenarioConfiguration.getconfigurationByKey(commonConstants.configurationKey.layerable).getValue()){
            this.layerManager = new LayerManager(v,scenarioConfiguration.getConfigurationsByType(commonConstants.dataOperateType.Layer));
            this.markerManager.setLayerManager(this.layerManager);
            this.troopManager.setLayerManager(this.layerManager);
            this.trackManager.setLayerManager(this.layerManager);
        }
    }


    getScenarioId(){
        return this.scenarioId;
    }

    setScenarioId(id){
        this.scenarioId =id; 
    }

    /**
     * 
     * @returns {MarkerManager}
     */
    getMarkerManager(){
        return this.markerManager;
    }

    getDecisionMaking(){
        return this.decisionMaking;
    }

    setDecisionMaking(de){
        this.decisionMaking = de;
    }

    /**
     * 
     * @returns {TroopManager}
     */
    getTroopManager(){
        return this.troopManager;
    }

    /**
     * 
     * @returns {TrackManager}
     */
    getTrackManager(){
        return this.trackManager;
    }

    getInfuenceSource(){
        return this.InfluenceSource;
    }

    getScenarioName(){
        return this.scenarioName;
    }

    setSceanrioName(name){
        this.scenarioName = name;
    }

    getCreateTime(){
        return this.createTime;
    }

    getCreateTimeString(){
        return this.sceanrioCreateTime;
    }

    //如果是字符串在进行处理后赋值
    setCreateTime(t){
        this.createTime = t;
        this.sceanrioCreateTime  = commonMethod.dateFormat(this.createTime);
    }

    getScenarioDescription(){
        return this.scenarioDescription;
    }

    setScenarioDescription(desc){
        this.scenarioDescription = desc;
    }

    /**
     * 部署到地图,这里就是重新加载
     */
    deploy(){

    }

    /**
     * 将部署到地图的模型等消除
     */
    destroy(){
        this.markerManager.destroy();
        this.troopManager.destroy();
    }

}

export default ScenarioManager;